home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 1998 November / IRIX 6.5.2 Base Documentation November 1998.img / usr / share / catman / u_man / cat1 / perlform.z / perlform
Text File  |  1998-10-30  |  18KB  |  463 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))                                                        PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perlform - Perl formats
  10.  
  11. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.      Perl has a mechanism to help you generate simple reports and charts.  To
  13.      facilitate this, Perl helps you code up your output page close to how it
  14.      will look when it's printed.  It can keep track of things like how many
  15.      lines are on a page, what page you're on, when to print page headers,
  16.      etc.  Keywords are borrowed from FORTRAN: _f_o_r_m_a_t() to declare and _w_r_i_t_e()
  17.      to execute; see their entries in the _p_e_r_l_f_u_n_c manpage.  Fortunately, the
  18.      layout is much more legible, more like BASIC's PRINT USING statement.
  19.      Think of it as a poor man's _n_r_o_f_f(1).
  20.  
  21.      Formats, like packages and subroutines, are declared rather than
  22.      executed, so they may occur at any point in your program.  (Usually it's
  23.      best to keep them all together though.) They have their own namespace
  24.      apart from all the other "types" in Perl.  This means that if you have a
  25.      function named "Foo", it is not the same thing as having a format named
  26.      "Foo".  However, the default name for the format associated with a given
  27.      filehandle is the same as the name of the filehandle.  Thus, the default
  28.      format for STDOUT is name "STDOUT", and the default format for filehandle
  29.      TEMP is name "TEMP".  They just look the same.  They aren't.
  30.  
  31.      Output record formats are declared as follows:
  32.  
  33.          format NAME =
  34.          FORMLIST
  35.          .
  36.  
  37.      If name is omitted, format "STDOUT" is defined.  FORMLIST consists of a
  38.      sequence of lines, each of which may be one of three types:
  39.  
  40.      1.  A comment, indicated by putting a '#' in the first column.
  41.  
  42.      2.  A "picture" line giving the format for one output line.
  43.  
  44.      3.  An argument line supplying values to plug into the previous picture
  45.          line.
  46.  
  47.      Picture lines are printed exactly as they look, except for certain fields
  48.      that substitute values into the line.  Each field in a picture line
  49.      starts with either "@" (at) or "^" (caret).  These lines do not undergo
  50.      any kind of variable interpolation.  The at field (not to be confused
  51.      with the array marker @) is the normal kind of field; the other kind,
  52.      caret fields, are used to do rudimentary multi-line text block filling.
  53.      The length of the field is supplied by padding out the field with
  54.      multiple "<", ">", or "|" characters to specify, respectively, left
  55.      justification, right justification, or centering.  If the variable would
  56.      exceed the width specified, it is truncated.
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))                                                        PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  71.  
  72.  
  73.  
  74.      As an alternate form of right justification, you may also use "#"
  75.      characters (with an optional ".") to specify a numeric field.  This way
  76.      you can line up the decimal points.  If any value supplied for these
  77.      fields contains a newline, only the text up to the newline is printed.
  78.      Finally, the special field "@*" can be used for printing multi-line,
  79.      nontruncated values; it should appear by itself on a line.
  80.  
  81.      The values are specified on the following line in the same order as the
  82.      picture fields.  The expressions providing the values should be separated
  83.      by commas.  The expressions are all evaluated in a list context before
  84.      the line is processed, so a single list expression could produce multiple
  85.      list elements.  The expressions may be spread out to more than one line
  86.      if enclosed in braces.  If so, the opening brace must be the first token
  87.      on the first line.  If an expression evaluates to a number with a decimal
  88.      part, and if the corresponding picture specifies that the decimal part
  89.      should appear in the output (that is, any picture except multiple "#"
  90.      characters wwwwiiiitttthhhhoooouuuutttt an embedded "."), the character used for the decimal
  91.      point is aaaallllwwwwaaaayyyyssss determined by the current LC_NUMERIC locale.  This means
  92.      that, if, for example, the run-time environment happens to specify a
  93.      German locale, "," will be used instead of the default ".".  See the
  94.      _p_e_r_l_l_o_c_a_l_e manpage and the section on _W_A_R_N_I_N_G_S for more information.
  95.  
  96.      Picture fields that begin with ^ rather than @ are treated specially.
  97.      With a # field, the field is blanked out if the value is undefined.  For
  98.      other field types, the caret enables a kind of fill mode.  Instead of an
  99.      arbitrary expression, the value supplied must be a scalar variable name
  100.      that contains a text string.  Perl puts as much text as it can into the
  101.      field, and then chops off the front of the string so that the next time
  102.      the variable is referenced, more of the text can be printed.  (Yes, this
  103.      means that the variable itself is altered during execution of the _w_r_i_t_e()
  104.      call, and is not returned.)  Normally you would use a sequence of fields
  105.      in a vertical stack to print out a block of text.  You might wish to end
  106.      the final field with the text "...", which will appear in the output if
  107.      the text was too long to appear in its entirety.  You can change which
  108.      characters are legal to break on by changing the variable $: (that's
  109.      $FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a
  110.      list of the desired characters.
  111.  
  112.      Using caret fields can produce variable length records.  If the text to
  113.      be formatted is short, you can suppress blank lines by putting a "~"
  114.      (tilde) character anywhere in the line.  The tilde will be translated to
  115.      a space upon output.  If you put a second tilde contiguous to the first,
  116.      the line will be repeated until all the fields on the line are exhausted.
  117.      (If you use a field of the at variety, the expression you supply had
  118.      better not give the same value every time forever!)
  119.  
  120.      Top-of-form processing is by default handled by a format with the same
  121.      name as the current filehandle with "_TOP" concatenated to it.  It's
  122.      triggered at the top of each page.  See the write entry in the _p_e_r_l_f_u_n_c
  123.      manpage.
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))                                                        PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  137.  
  138.  
  139.  
  140.      Examples:
  141.  
  142.       # a report on the /etc/passwd file
  143.       format STDOUT_TOP =
  144.                               Passwd File
  145.       Name                Login    Office   Uid   Gid Home
  146.       ------------------------------------------------------------------
  147.       .
  148.       format STDOUT =
  149.       @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
  150.       $name,              $login,  $office,$uid,$gid, $home
  151.       .
  152.  
  153.       # a report from a bug report form
  154.       format STDOUT_TOP =
  155.                               Bug Reports
  156.       @<<<<<<<<<<<<<<<<<<<<<<<     @|||         @>>>>>>>>>>>>>>>>>>>>>>>
  157.       $system,                      $%,         $date
  158.       ------------------------------------------------------------------
  159.       .
  160.       format STDOUT =
  161.       Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  162.                $subject
  163.       Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  164.              $index,                       $description
  165.       Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  166.                 $priority,        $date,   $description
  167.       From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  168.             $from,                         $description
  169.       Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  170.                    $programmer,            $description
  171.       ~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  172.                                            $description
  173.       ~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  174.                                            $description
  175.       ~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  176.                                            $description
  177.       ~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  178.                                            $description
  179.       ~                                    ^<<<<<<<<<<<<<<<<<<<<<<<...
  180.                                            $description
  181.       .
  182.  
  183.      It is possible to intermix _p_r_i_n_t()s with _w_r_i_t_e()s on the same output
  184.      channel, but you'll have to handle $- ($FORMAT_LINES_LEFT) yourself.
  185.  
  186.      FFFFoooorrrrmmmmaaaatttt VVVVaaaarrrriiiiaaaabbbblllleeeessss
  187.  
  188.      The current format name is stored in the variable $~ ($FORMAT_NAME), and
  189.      the current top of form format name is in $^ ($FORMAT_TOP_NAME).  The
  190.      current output page number is stored in $% ($FORMAT_PAGE_NUMBER), and the
  191.      number of lines on the page is in $= ($FORMAT_LINES_PER_PAGE).  Whether
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))                                                        PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  203.  
  204.  
  205.  
  206.      to autoflush output on this handle is stored in $| ($OUTPUT_AUTOFLUSH).
  207.      The string output before each top of page (except the first) is stored in
  208.      $^L ($FORMAT_FORMFEED).  These variables are set on a per-filehandle
  209.      basis, so you'll need to _s_e_l_e_c_t() into a different one to affect them:
  210.  
  211.          select((select(OUTF),
  212.                  $~ = "My_Other_Format",
  213.                  $^ = "My_Top_Format"
  214.                 )[0]);
  215.  
  216.      Pretty ugly, eh?  It's a common idiom though, so don't be too surprised
  217.      when you see it.  You can at least use a temporary variable to hold the
  218.      previous filehandle: (this is a much better approach in general, because
  219.      not only does legibility improve, you now have intermediary stage in the
  220.      expression to single-step the debugger through):
  221.  
  222.          $ofh = select(OUTF);
  223.          $~ = "My_Other_Format";
  224.          $^ = "My_Top_Format";
  225.          select($ofh);
  226.  
  227.      If you use the English module, you can even read the variable names:
  228.  
  229.          use English;
  230.          $ofh = select(OUTF);
  231.          $FORMAT_NAME     = "My_Other_Format";
  232.          $FORMAT_TOP_NAME = "My_Top_Format";
  233.          select($ofh);
  234.  
  235.      But you still have those funny _s_e_l_e_c_t()s.  So just use the FileHandle
  236.      module.  Now, you can access these special variables using lowercase
  237.      method names instead:
  238.  
  239.          use FileHandle;
  240.          format_name     OUTF "My_Other_Format";
  241.          format_top_name OUTF "My_Top_Format";
  242.  
  243.      Much better!
  244.  
  245. NNNNOOOOTTTTEEEESSSS
  246.      Because the values line may contain arbitrary expressions (for at fields,
  247.      not caret fields), you can farm out more sophisticated processing to
  248.      other functions, like _s_p_r_i_n_t_f() or one of your own.  For example:
  249.  
  250.          format Ident =
  251.              @<<<<<<<<<<<<<<<
  252.              &commify($n)
  253.          .
  254.  
  255.      To get a real at or caret into the field, do this:
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))                                                        PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  269.  
  270.  
  271.  
  272.          format Ident =
  273.          I have an @ here.
  274.                  "@"
  275.          .
  276.  
  277.      To center a whole line of text, do something like this:
  278.  
  279.          format Ident =
  280.          @|||||||||||||||||||||||||||||||||||||||||||||||
  281.                  "Some text line"
  282.          .
  283.  
  284.      There is no builtin way to say "float this to the right hand side of the
  285.      page, however wide it is."  You have to specify where it goes.  The truly
  286.      desperate can generate their own format on the fly, based on the current
  287.      number of columns, and then _e_v_a_l() it:
  288.  
  289.          $format  = "format STDOUT = \n";
  290.                   . '^' . '<' x $cols . "\n";
  291.                   . '$entry' . "\n";
  292.                   . "\t^" . "<" x ($cols-8) . "~~\n";
  293.                   . '$entry' . "\n";
  294.                   . ".\n";
  295.          print $format if $Debugging;
  296.          eval $format;
  297.          die $@ if $@;
  298.  
  299.      Which would generate a format looking something like this:
  300.  
  301.       format STDOUT =
  302.       ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  303.       $entry
  304.               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
  305.       $entry
  306.       .
  307.  
  308.      Here's a little program that's somewhat like _f_m_t(1):
  309.  
  310.       format =
  311.       ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
  312.       $_
  313.  
  314.       .
  315.  
  316.       $/ = '';
  317.       while (<>) {
  318.           s/\s*\n\s*/ /g;
  319.           write;
  320.       }
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))                                                        PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  335.  
  336.  
  337.  
  338.      FFFFooooooootttteeeerrrrssss
  339.  
  340.      While $FORMAT_TOP_NAME contains the name of the current header format,
  341.      there is no corresponding mechanism to automatically do the same thing
  342.      for a footer.  Not knowing how big a format is going to be until you
  343.      evaluate it is one of the major problems.  It's on the TODO list.
  344.  
  345.      Here's one strategy:  If you have a fixed-size footer, you can get
  346.      footers by checking $FORMAT_LINES_LEFT before each _w_r_i_t_e() and print the
  347.      footer yourself if necessary.
  348.  
  349.      Here's another strategy: Open a pipe to yourself, using open(MYSELF, "|-
  350.      ") (see the open() entry in the _p_e_r_l_f_u_n_c manpage) and always _w_r_i_t_e() to
  351.      MYSELF instead of STDOUT.  Have your child process massage its STDIN to
  352.      rearrange headers and footers however you like.  Not very convenient, but
  353.      doable.
  354.  
  355.      AAAAcccccccceeeessssssssiiiinnnngggg FFFFoooorrrrmmmmaaaattttttttiiiinnnngggg IIIInnnntttteeeerrrrnnnnaaaallllssss
  356.  
  357.      For low-level access to the formatting mechanism.  you may use _f_o_r_m_l_i_n_e()
  358.      and access $^A (the $ACCUMULATOR variable) directly.
  359.  
  360.      For example:
  361.  
  362.          $str = formline <<'END', 1,2,3;
  363.          @<<<  @|||  @>>>
  364.          END
  365.  
  366.          print "Wow, I just stored `$^A' in the accumulator!\n";
  367.  
  368.      Or to make an _s_w_r_i_t_e() subroutine which is to _w_r_i_t_e() what _s_p_r_i_n_t_f() is
  369.      to _p_r_i_n_t_f(), do this:
  370.  
  371.          use Carp;
  372.          sub swrite {
  373.              croak "usage: swrite PICTURE ARGS" unless @_;
  374.              my $format = shift;
  375.              $^A = "";
  376.              formline($format,@_);
  377.              return $^A;
  378.          }
  379.  
  380.          $string = swrite(<<'END', 1, 2, 3);
  381.       Check me out
  382.       @<<<  @|||  @>>>
  383.       END
  384.          print $string;
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))                                                        PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  401.  
  402.  
  403.  
  404. WARNINGS
  405.      The lone dot that ends a format can also prematurely end an email message
  406.      passing through a misconfigured Internet mailer (and based on experience,
  407.      such misconfiguration is the rule, not the exception).  So when sending
  408.      format code through email, you should indent it so that the format-ending
  409.      dot is not on the left margin; this will prevent email cutoff.
  410.  
  411.      Lexical variables (declared with "my") are not visible within a format
  412.      unless the format is declared within the scope of the lexical variable.
  413.      (They weren't visible at all before version 5.001.)
  414.  
  415.      Formats are the only part of Perl which unconditionally use information
  416.      from a program's locale; if a program's environment specifies an
  417.      LC_NUMERIC locale, it is always used to specify the decimal point
  418.      character in formatted output.  Perl ignores all other aspects of locale
  419.      handling unless the use locale pragma is in effect.  Formatted output
  420.      cannot be controlled by use locale because the pragma is tied to the
  421.      block structure of the program, and, for historical reasons, formats
  422.      exist outside that block structure.  See the _p_e_r_l_l_o_c_a_l_e manpage for
  423.      further discussion of locale handling.
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.